//
//  RadiateSettingViewController.m
//  JunZeWristband
//
//  Created by Kendama on 2017/9/12.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "RadiateSettingViewController.h"
#import "CustomPickerView.h"
#import "SetUserInfoApi.h"

@interface RadiateSettingViewController ()<CustomPickerViewDelegate>

@property(nonatomic,strong) UILabel *tipLabel;
@property(nonatomic,strong) CustomPickerView *customView;
@property(nonatomic,strong) NSMutableArray *dataSource;
@property(nonatomic,copy) NSString *selectTarger;

@end

@implementation RadiateSettingViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.selectTarger = @"0.0";
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)dealloc
{
    
}

- (void)refreshBaseControlValue
{
    
}

#pragma mark - 点击事件
- (void)onRightButtonClick:(id)sender
{
    NSString *hex = [NSString stringZeroPaddingbringToFront:[NSString decimalToHexadecimal:(int)([self.selectTarger floatValue]*800)] Length:4];
    NSString *code = [NSString stringWithFormat:@"FC2002%@000000000000000000000000000000",hex];
    DTCDeviceModel *device = [DTCBluetoothManager shareInstance].connectedDeviceList.lastObject;
    NSData *sendData = [NSString convertHexStrToData:code];
    [[DTCBlueToothSendData sharedInstance] sendDataByData:sendData andInfo:device];
    
    SetUserInfoApi * setApi = [[SetUserInfoApi alloc] initWithInfo:@{@"bracelet_radiation_limit":self.selectTarger}];
    [setApi dtDefaultInterceptStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
        NSDictionary *dict = [request responseJSONObject];
        if ([dict[@"errcode"] intValue] == 0)
        {
            [SVProgressHUD showSuccessWithStatus:MyNSLocalizedString(@"设置成功")];
            SAVE_OBJECT(self.selectTarger, BraceletRadiationLimit);
            [self.navigationController popViewControllerAnimated:YES];
        }
    } failure:nil];
}

#pragma mark - 事件处理

#pragma mark - 代理
- (void)customPickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    self.selectTarger = self.dataSource[row];
    NSLog(@"=======保存成功=======");
    
}

#pragma mark - 通知处理

#pragma mark - 初始化
- (void)setNavigationStyle
{
    [self setNavigationBarTitle:MyNSLocalizedString(@"上限设置")];
    [self setRightButtonTitle:MyNSLocalizedString(@"保存") backgroundImage:imageWithName(@"nav_btn_bg")];
}

- (UILabel *)tipLabel
{
    if(!_tipLabel)
    {
        UIView *superView = self.view;
        _tipLabel = [[UILabel alloc]init];
        [superView addSubview:_tipLabel];
        [_tipLabel setFont:[UIFont systemFontOfSize:15]];
        [_tipLabel setTextColor:[UIColor darkGrayColor]];
        _tipLabel.textAlignment = NSTextAlignmentCenter;
        _tipLabel.numberOfLines = 0;
        [_tipLabel sizeToFit];
        _tipLabel.text = MyNSLocalizedString(@"根据WHO的建议，当环境辐射值大于0.4时，可能对人体造成伤害");
    }
    return _tipLabel;
}


- (CustomPickerView *)customView
{
    if(!_customView)
    {
        UIView *superView = self.view;
        _customView = [[CustomPickerView alloc] init];
        _customView.customPickerViewDelegate = self;
        _customView.datasource = [NSMutableArray arrayWithArray:self.dataSource];
        _customView.placeLabel.text = @"μT";
        _customView.shadeView.backgroundColor = colorWithAlpha(248, 165, 165, 1);
        [_customView showLocationByValue:EXRACT_OBJECT(BraceletRadiationLimit)];
        [superView addSubview:_customView];
    }
    return _customView;
}

- (NSMutableArray *)dataSource
{
    if(!_dataSource)
    {
        _dataSource = [[NSMutableArray alloc]init];
        for (int i = 0; i <= 100; i++) {
            NSString *string = [NSString stringWithFormat:@"%.1f",i*0.1];
            [_dataSource addObject:string];
        }
    }
    return _dataSource;
}


- (void)masLayoutSubview
{
    __weak UIView *superView = self.view;
    [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(64+px1080Width(73));
        make.left.mas_equalTo(superView.mas_left).with.offset(px1080Width(110));
        make.right.mas_equalTo(superView.mas_right).with.offset(px1080Width(-110));
    }];

    [self.customView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.tipLabel.mas_bottom).with.offset(px1920Height(80));
        make.left.right.mas_equalTo(superView);
        make.height.mas_equalTo(px1920Height(900));
    }];
    

}

@end
